package com.example.demo_springboot.controller;

import com.example.demo_springboot.service.AccountService;
import com.example.demo_springboot.service.AccoutMyBatisService;
import com.example.demo_springboot.service.IAccountService;
import com.example.demo_springboot.vo.Account;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/*
 * 基于 mybatis 的一个测试案例
 * */

@RestController
@RequestMapping("/accountmybatis")
public class AccountMybatisController {
    @Autowired
    AccoutMyBatisService accountService;

    @ApiOperation(value="查询", notes="查询所有清单")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    @ApiOperation(value="根据ID查询", notes="查询所有清单where id=?")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccount(id);
    }

    //需要说明的是，如果请求参数在url上，@ApiImplicitParam 上加paramType = “path”
//    paramType：表示参数放在哪个地方
    //    header-->请求参数的获取：@RequestHeader(代码中接收注解)
    //    query-->请求参数的获取：@RequestParam(代码中接收注解)
    //    path（用于restful接口）-->请求参数的获取：@PathVariable(代码中接收注解)
    //    body-->请求参数的获取：@RequestBody(代码中接收注解)
    //    form（不常用）
    @ApiOperation(value="更新信息", notes="根据url的id来指定更新Account信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Account Id", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "money", value = "money", required = true, dataType = "Double",paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id,
                                @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t= accountService.update(name,money,id);
        if(t==1) {
            return "success ";
        }else {
            return "fail ";
        }

    }

    @ApiIgnore //这个标示不生成 swagger2 文档
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public int updateByXml(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t = 0;
        try{
            t= accountService.updateByXml(name,money,id);
        }catch (Exception e){
            t = -1;
        }
        return t;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= accountService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {

        int t= accountService.add(name,money);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
        Account acc = new Account();
        acc.setName(name);
        acc.setMoney(money);
        int t = this.accountService.addObj(acc);
        if(t==1) {
            return "success Obj";
        }else {
            return "fail Obj";
        }

    }

    @RequestMapping(value="/findByName", method=RequestMethod.GET)
    public Account findUserByName(@RequestParam(value="name") String name){
        Account account = accountService.findByName(name);
        return account;
    }
}
